package net.qsge.common.oauth;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthException;
import net.oauth.OAuthMessage;
import net.oauth.OAuthServiceProvider;
import net.oauth.client.OAuthClient;
import net.oauth.client.httpclient4.HttpClient4;

/**
 * Servlet implementation class CallBack
 */
public class CallBack extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CallBack() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String oauthVerifier = request.getParameter("oauth_verifier");
		String oauthToken = request.getParameter("oauth_token");
		
		OAuthClient client = new OAuthClient(new HttpClient4());

		OAuthConsumer consumer = new OAuthConsumer("", "mangaonweb.com",
				"t2opOGveWrpfeVrSZFzCh8Gk", new OAuthServiceProvider(
						"https://www.google.com/accounts/OAuthGetRequestToken",
						"https://www.google.com/accounts/OAuthAuthorizeToken",
						"https://www.google.com/accounts/OAuthGetAccessToken"));
		OAuthAccessor accessor = new OAuthAccessor(consumer);
		
		
		Map<String, String> result = (Map<String, String>) request.getSession().getAttribute("accessor");

		accessor.tokenSecret = result.get("tokenSecret");

		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("oauth_token", result.get("requestToken"));
		parameters.put("oauth_verifier", oauthVerifier);
		
		OAuthMessage responseText;
		try {
			responseText = client.getAccessToken(accessor, "GET", parameters.entrySet());

			Map<String, String> ret = new HashMap<String, String>();
			ret.put("accessToken", responseText.getParameter("oauth_token"));
			ret.put("tokenSecret", responseText.getParameter("oauth_token_secret"));

			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(this.getText(responseText.getParameter("oauth_token"), responseText.getParameter("oauth_token_secret")));
			
		} catch (OAuthException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	private String getText(String accessToken, String tokenSecret) throws IOException, OAuthException, URISyntaxException {
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("oauth_token", accessToken);
		
		
		OAuthClient client = new OAuthClient(new HttpClient4());

		OAuthConsumer consumer = new OAuthConsumer("", "mangaonweb.com",
				"t2opOGveWrpfeVrSZFzCh8Gk", new OAuthServiceProvider(
						"https://www.google.com/accounts/OAuthGetRequestToken",
						"https://www.google.com/accounts/OAuthAuthorizeToken",
						"https://www.google.com/accounts/OAuthGetAccessToken"));
		OAuthAccessor accessor = new OAuthAccessor(consumer);
		

		accessor.tokenSecret = tokenSecret;

		OAuthMessage response = client.invoke(accessor, "https://www.google.com/m8/feeds/contacts/default/full", parameters.entrySet());

		return response.readBodyAsString();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
